﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cn.jpush.api;
using cn.jpush.api.push.mode;
using cn.jpush.api.common;
using cn.jpush.api.common.resp;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using cn.jpush.api.push.notification;
using cn.jpush.api.device;
using cn.jpush.api.push;

namespace Push
{
    public class JPush
    {
        public static String TITLE = "Test from C# v3 sdk";
        public static String ALERT = "Test from  C# v3 sdk - alert";
        public static String MSG_CONTENT = "Test from C# v3 sdk - msgContent";
        public static String REGISTRATION_ID = "0900e8d85ef";
        public static String TAG = "tag_api";
        public static String app_key = "512a814023efd6c9c83a7769";
        public static String master_secret = "40e063f01c4d4db3093c1d76";
        public static String SMSMESSAGE = "Test from C# v3 sdk - SMSMESSAGE";
        public static int DELAY_TIME = 1;
        /// <summary>
        /// 根据别名推送消息
        /// </summary>
        /// <param name="alias">别名</param>
        /// <param name="content">消息内容</param>
        public void PushByAlias(string alias, string content)
        {
            PushPayload payload = PushObject_android_and_ios();
            JPushClient client = new JPushClient(app_key, master_secret);
            payload.audience = Audience.s_alias(alias);
            payload.notification.setAlert(content);

            try
            {
                MessageResult result = client.SendPush(payload);
                LogDebug.Debug("推送消息成功！", result.msg_id.ToString());
            }
            catch (APIRequestException e)
            {
                LogDebug.Debug("推送消息失败：", "HTTP Status: " + e.Status);
                LogDebug.Debug("Error Code", e.ErrorCode.ToString());
                LogDebug.Debug("Error Message", e.ErrorMessage);
            }
            catch (APIConnectionException e)
            {
                LogDebug.Debug("推送消息失败：", "Error Message: " + e.Message);
            }
        }
        /// <summary>
        /// 根据别名推送消息
        /// </summary>
        /// <param name="alias">别名</param>
        /// <param name="content">消息内容</param>
        /// <param name="param">自定义参数列表</param>
        public void PushByAlias(string alias, string content, Dictionary<string, string> param)
        {
            JPushClient client = new JPushClient(app_key, master_secret);
            PushPayload payload = PushObject_android_and_ios(param);
            payload.audience = Audience.s_alias(alias);
            payload.notification.setAlert(content);
            try
            {
                MessageResult result = client.SendPush(payload);
                LogDebug.Debug("推送消息成功！", result.msg_id.ToString());
            }
            catch (APIRequestException e)
            {
                LogDebug.Debug("推送消息失败：", "HTTP Status: " + e.Status);
                LogDebug.Debug("Error Code", e.ErrorCode.ToString());
                LogDebug.Debug("Error Message", e.ErrorMessage);
            }
            catch (APIConnectionException e)
            {
                LogDebug.Debug("推送消息失败：", "Error Message: " + e.Message);
            }
        }
        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public AliasDeviceListResult GetAliasDevice(string alias)
        {
            DeviceClient client = new DeviceClient(app_key, master_secret);
            try
            {
                AliasDeviceListResult result = client.getAliasDeviceList(alias, null);
                return result;
            }
            catch (Exception ex)
            {
                return new AliasDeviceListResult();
            }

        }

        //测试
        public void PushTest()
        {
            PushPayload payload = PushObject_All_All_Alert();
            JPushClient client = new JPushClient(app_key, master_secret);
            try
            {

                var result = client.SendPush(payload);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                //如需查询上次推送结果执行下面的代码
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                //如需查询某个messageid的推送结果执行下面的代码
                var queryResultWithV2 = client.getReceivedApi("1739302794");
                var querResultWithV3 = client.getReceivedApi_v3("1739302794");

            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }

            //send   smsmessage
            PushPayload pushsms = PushSendSmsMessage();
            try
            {
                var result = client.SendPush(pushsms);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                //如需查询上次推送结果执行下面的代码
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                //如需查询某个messageid的推送结果执行下面的代码
                var queryResultWithV2 = client.getReceivedApi("1739302794");
                var querResultWithV3 = client.getReceivedApi_v3("1739302794");

            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }

            PushPayload payload_alias = PushObject_all_alias_alert();
            try
            {
                var result = client.SendPush(payload_alias);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                //如需查询上次推送结果执行下面的代码
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                //如需查询某个messageid的推送结果执行下面的代码
                var queryResultWithV2 = client.getReceivedApi("1739302794");
                var querResultWithV3 = client.getReceivedApi_v3("1739302794");

            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public void DelAlias(string alias)
        {
            try
            {
                DeviceClient DC = new DeviceClient(app_key, master_secret);
                DC.deleteAlias(alias, null);
            }
            catch (APIRequestException e)
            {
                LogDebug.Debug("删除别名：", "HTTP Status: " + e.Status);
                LogDebug.Debug("Error Code", e.ErrorCode.ToString());
                LogDebug.Debug("Error Message", e.ErrorMessage);
            }
            catch (APIConnectionException e)
            {
                LogDebug.Debug("删除别名：", "Error Message: " + e.Message);
            }
        }
        public static PushPayload PushObject_All_All_Alert()
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.all();
            pushPayload.notification = new Notification().setAlert(ALERT);
            return pushPayload;
        }
        public static PushPayload PushObject_all_alias_alert()
        {

            PushPayload pushPayload_alias = new PushPayload();
            pushPayload_alias.platform = Platform.android();
            pushPayload_alias.audience = Audience.s_alias("alias1");
            pushPayload_alias.notification = new Notification().setAlert(ALERT);
            return pushPayload_alias;
        }
        public static PushPayload PushObject_Android_Tag_AlertWithTitle()
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.android();
            pushPayload.audience = Audience.s_tag("tag1");
            pushPayload.notification = Notification.android(ALERT, TITLE);
            return pushPayload;
        }
        public static PushPayload PushObject_android_and_ios()
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.android_ios();
            var audience = Audience.s_tag("tag1");
            pushPayload.audience = audience;
            var notification = new Notification().setAlert("alert content");
            notification.AndroidNotification = new AndroidNotification().setTitle("Antiwear");
            notification.IosNotification = new IosNotification();
            notification.IosNotification.setSound("default");
            notification.IosNotification.incrBadge(1);
            notification.IosNotification.AddExtra("extra_key", "extra_value");
            pushPayload.notification = notification.Check();
            pushPayload.options.apns_production = true;
            return pushPayload;
        }
        public static PushPayload PushObject_android_and_ios(Dictionary<string, string> param)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.android_ios();
            var audience = Audience.s_tag("tag1");
            pushPayload.audience = audience;
            var notification = new Notification().setAlert("alert content");
            notification.AndroidNotification = new AndroidNotification().setTitle("Antiwear");
            notification.IosNotification = new IosNotification();
            notification.IosNotification.setSound("default");
            notification.IosNotification.incrBadge(1);
            foreach (KeyValuePair<string, string> item in param)
            {
                notification.IosNotification.AddExtra(item.Key, item.Value);
                notification.AndroidNotification.AddExtra(item.Key, item.Value);
            }
            pushPayload.notification = notification.Check();
            pushPayload.options.apns_production = true;
            return pushPayload;
        }
        public static PushPayload PushObject_ios_tagAnd_alertWithExtrasAndMessage()
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.android_ios();
            pushPayload.audience = Audience.s_tag_and("tag1", "tag_all");
            var notification = new Notification();
            notification.IosNotification = new IosNotification().setAlert(ALERT).setBadge(5).setSound("happy").AddExtra("from", "JPush");

            pushPayload.notification = notification;
            pushPayload.message = Message.content(MSG_CONTENT);
            return pushPayload;

        }
        public static PushPayload PushObject_ios_audienceMore_messageWithExtras()
        {

            var pushPayload = new PushPayload();
            pushPayload.platform = Platform.android_ios();
            pushPayload.audience = Audience.s_tag("tag1", "tag2");
            pushPayload.message = Message.content(MSG_CONTENT).AddExtras("from", "JPush");
            return pushPayload;

        }

        public static PushPayload PushSendSmsMessage()
        {
            var pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.all();
            pushPayload.notification = new Notification().setAlert(ALERT);
            SmsMessage sms_message = new SmsMessage();
            sms_message.setContent(SMSMESSAGE);
            sms_message.setDelayTime(DELAY_TIME);
            pushPayload.sms_message = sms_message;
            return pushPayload;
        }
    }

}
